
// VideoDlg.cpp : implementation file
//

#include "pch.h"
#include "framework.h"
#include "Video.h"
#include "VideoDlg.h"
#include "afxdialogex.h"
#include "video_api.h"
#include "register_drivers.h"
#include "def.h"

#define REC_NAME	_T("C:\\shenju\\shenju\\yp3\\data\\Video\\record.avi")

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CVideoDlg dialog



CVideoDlg::CVideoDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_VIDEO_DIALOG, pParent)
	, m_FilePath(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_bPlay = -1;
	m_Width = 240;
	m_Height = 320;
}

void CVideoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_SCREEN, m_Screen);
	DDX_Text(pDX, IDC_EDIT1, m_FilePath);
}

BEGIN_MESSAGE_MAP(CVideoDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, &CVideoDlg::OnBnClickedButton1)
	ON_BN_CLICKED(IDC_BUTTON2, &CVideoDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON3, &CVideoDlg::OnBnClickedButton3)
	ON_BN_CLICKED(IDC_BUTTON4, &CVideoDlg::OnBnClickedButton4)
	ON_BN_CLICKED(IDC_BUTTON5, &CVideoDlg::OnBnClickedButton5)
	ON_BN_CLICKED(IDC_BUTTON6, &CVideoDlg::OnBnClickedButton6)
	ON_MESSAGE(WM_VIDEOCB, &CVideoDlg::OnVideoCB)
END_MESSAGE_MAP()


// CVideoDlg message handlers

BOOL CVideoDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	SetScreenSize(m_Width, m_Height);

	register_drivers();

	return TRUE;  // return TRUE  unless you set the focus to a control
}


void CVideoDlg::OnOK()
{
	//CDialogEx::OnOK();
}

void CVideoDlg::OnCancel()
{
	OnBnClickedButton3();

	unregister_drivers();

	CDialogEx::OnCancel();
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CVideoDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}


void CVideoDlg::SetScreenSize(int w, int h)
{
	CRect rct;
	m_Screen.GetWindowRect(&rct);
	ScreenToClient(&rct);
	m_Screen.MoveWindow(rct.left, rct.top, w, h);
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CVideoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void VideoCB(VIDEO_STA sta, u32 param)
{
	CWnd* pWnd = ::AfxGetApp()->GetMainWnd();
	pWnd->PostMessage(WM_VIDEOCB, (WPARAM)sta, (LPARAM)param);
}

LRESULT CVideoDlg::OnVideoCB(WPARAM sta, LPARAM lp)
{
	switch (sta)
	{
	case VIDEO_HEAD:
		TRACE("VIDEO_HEAD = %d\n", lp);
		break;
	case VIDEO_PROGRESS:
		TRACE("VIDEO_PROGRESS = %d\n", lp);
		break;
	case VIDEO_FRAME_A:
		TRACE("VIDEO_FRAME_A = %d\n", lp);
		break;
	case VIDEO_FRAME_V:
		TRACE("VIDEO_FRAME_V = %d\n", lp);
		break;
	case VIDEO_OVER:
		TRACE("VIDEO_OVER = %d\n", lp);
		OnBnClickedButton3();
		break;
	case VIDEO_STREAM_ERR:
		TRACE("VIDEO_STREAM_ERR = %d\n", lp);
		OnBnClickedButton3();
		AfxMessageBox(_T("VIDEO_STREAM_ERR"));
		break;
	case VIDEO_VIDEO_SIZE:
		m_Width = LOWORD(lp);
		m_Height = HIWORD(lp);
		SetScreenSize(m_Width, m_Height);
		break;
	default:
		ASSERT(0);
		break;
	}
	return 0;
}

void CVideoDlg::OnBnClickedButton1()
{
	CString strFilter = _T("video file (*.avi)|*.avi||");

	CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFilter);

	if (IDOK == dlg.DoModal())
	{
		m_FilePath = dlg.GetPathName();
		UpdateData(0);
		VIDEO_ERROR res = play_start(m_FilePath.GetBuffer(), ::VideoCB);
		if (res == VIDEO_ERROR_OK)
		{
			m_bPlay = 1;
			GetDlgItem(IDC_BUTTON1)->EnableWindow(1);
			GetDlgItem(IDC_BUTTON2)->EnableWindow(1);
			GetDlgItem(IDC_BUTTON3)->EnableWindow(1);
			GetDlgItem(IDC_BUTTON4)->EnableWindow(1);
			GetDlgItem(IDC_BUTTON5)->EnableWindow(1);
			GetDlgItem(IDC_BUTTON6)->EnableWindow(1);
		}
		else
		{
			AfxMessageBox("play_start error !");
		}
	}
}

void CVideoDlg::OnBnClickedButton2()
{
	char avi_name[MAX_PATH];
	GetModuleFileName(NULL, avi_name, MAX_PATH);
	PathRemoveFileSpec(avi_name);
	PathRemoveFileSpec(avi_name);
	strcat_s(avi_name, MAX_PATH, "\\data\\Video\\record.avi");
	m_FilePath = avi_name;// REC_NAME;
	UpdateData(0);

	VIDEO_ERROR res = record_start(m_FilePath.GetBuffer(), ::VideoCB);
	if (res == VIDEO_ERROR_OK)
	{
		m_bPlay = 0;
		GetDlgItem(IDC_BUTTON1)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON2)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON3)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON4)->EnableWindow(1);
		GetDlgItem(IDC_BUTTON5)->EnableWindow(0);
		GetDlgItem(IDC_BUTTON6)->EnableWindow(0);
	}
	else
	{
		AfxMessageBox("record_start error !");
	}
}


void CVideoDlg::OnBnClickedButton3()
{
	if (m_bPlay == 1)
	{
		VIDEO_ERROR res = play_stop();
		if(res)AfxMessageBox("play_stop error !");
	}
	else if(m_bPlay == 0)
	{
		VIDEO_ERROR res = record_stop();
		if (res)AfxMessageBox("record_stop error !");
	}
	m_FilePath = _T("");
	UpdateData(0);

	m_bPlay = -1;
	GetDlgItem(IDC_BUTTON1)->EnableWindow(1);
	GetDlgItem(IDC_BUTTON2)->EnableWindow(1);
	GetDlgItem(IDC_BUTTON3)->EnableWindow(1);
	GetDlgItem(IDC_BUTTON4)->EnableWindow(1);
	GetDlgItem(IDC_BUTTON5)->EnableWindow(1);
	GetDlgItem(IDC_BUTTON6)->EnableWindow(1);
}


void CVideoDlg::OnBnClickedButton4()
{
	CString str;
	BOOL flag = TRUE;
	GetDlgItemText(IDC_BUTTON4, str);
	if (str == _T("Pause"))
	{
		flag = TRUE;
		str = _T("Resume");
		SetDlgItemText(IDC_BUTTON4, str);
		if (m_bPlay == 1)
		{
			play_pause();
		}
		else if (m_bPlay == 0)
		{
			record_pause();
		}
	}
	else
	{
		flag = FALSE;
		str = _T("Pause");
		SetDlgItemText(IDC_BUTTON4, str);
		if (m_bPlay == 1)
		{
			play_resume();
		}
		else if (m_bPlay == 0)
		{
			record_resume();
		}
	}
}


void CVideoDlg::OnBnClickedButton5()
{
	if (m_bPlay == 1)
	{
		play_seek(10);
	}
	else
	{
		ASSERT(0);
	}
}


void CVideoDlg::OnBnClickedButton6()
{
	if (m_bPlay == 1)
	{
		play_set_volume(100);
	}
	else
	{
		ASSERT(0);
	}
}
